package com.mall.controller;

import com.mall.dto.ActionResult;
import com.mall.dto.VSys_user;
import com.mall.po.Sys_user;
import com.mall.service.Sys_userService;
import com.mall.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author zhanggaoxiang
 * @ClassName 用户controller类
 * @Description TODO
 * @Date 2021/1/5 19:40
 * @Version 1.0
 **/
@Api(tags = "用户登录")
@RestController
@RequestMapping("/shangcheng")
@Slf4j
public class Sys_userController {
    @Autowired
    private Sys_userService sys_userService;
    /**
     * @Author zhanggaoxiang
     * @Description 用户登录
     * @Date 2021/1/6 15:55
     * @Param [vSys_user]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "用户登录")
    @ApiImplicitParam(name = "vSys_user",value = "用户索引的对象（json）",required = true)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ActionResult login(@RequestBody VSys_user vSys_user){
        System.out.println(vSys_user);
        Sys_user sys_user = sys_userService.findUserByUsername(vSys_user.getUsername());
        log.info("***************************"+sys_user);
        //判断用户名和密码
        if(sys_user!=null && sys_user.getUserpass().equals(vSys_user.getUserpass())){
            HashMap map = new HashMap();
            map.put("username",vSys_user.getUsername());
            String token = JwtUtils.sign(sys_user.getUsername());
            log.info("***************************"+token);
            map.put("token",token);
            return new ActionResult(1,"登陆成功！",map);
        }
        return  new ActionResult(0,"用户名或密码错误！",null);
    }
}
